package com.nice.nice201110.controller;

import com.nice.nice201110.entity.TUser;
import com.nice.nice201110.service.TuserService;
import com.nice.nice201110.utils.NiceEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.ValidationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/tuser")
public class TuserController {
    @Autowired
    TuserService tuserService;

    private Map<String,Object> codeMes = null;

    private HttpSession session = null;

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/list")
    public Map<String,Object> list(HttpServletRequest request, @RequestBody(required = false) BaseVo baseVo){
       session = request.getSession();
       codeMes = new HashMap<String,Object>();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code", NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        if(baseVo!=null){
            baseVo.setUservo(userVo);
        }else{
            baseVo = new BaseVo();
            baseVo.setUservo(userVo);
        }
        List<TUser> tUsers = tuserService.getList(baseVo);

        codeMes.put("code", NiceEnum.code_200);
        codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        codeMes.put("data", tUsers);
        return codeMes;

    }
    /**
     *   查询单一基本信息
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/one")
    public Map<String,Object> getOne(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id) {
        session = request.getSession();
        codeMes = new HashMap<String,Object>();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        TUser tUser = tuserService.getOne(userVo,id);
        if(tUser!=null){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tUser);
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    private void validData(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuffer sb = new StringBuffer();
            for (ObjectError error : bindingResult.getAllErrors()) {
                sb.append(error.getDefaultMessage());
            }
            throw new ValidationException(sb.toString());
        }
    }
    /** 基本信息插入 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value="/insert")
    public Map<String,Object> insert(HttpServletRequest request, @RequestBody @Validated TUser tUser, BindingResult bindingResult){
        validData(bindingResult);
        session = request.getSession();
        codeMes = new HashMap<String,Object>();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tuserService.insertTUser(userVo,tUser);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tUser);
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 基本信息更新 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT,value="/update")
    public Map<String,Object> update(HttpServletRequest request, TUser tUser){
        session = request.getSession();
        codeMes = new HashMap<String,Object>();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tuserService.updateTUser(userVo,tUser);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tUser);
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 删除信息 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value = "/deletes")
    public Map<String,Object> delete(HttpServletRequest request,@RequestParam(name = "ids",required=true)String ids){
        session = request.getSession();
        codeMes = new HashMap<String,Object>();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tuserService.deleteTUser(userVo,ids);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code", NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
}
